package com.github.dice.controller;

import javax.servlet.http.HttpServletRequest;

import com.github.dice.constant.ErrorEnums;
import com.github.dice.entity.Player;
import com.github.dice.entity.RoomOwner;
import com.github.dice.exception.BusinessException;

public class BaseController {

    protected Player getPlayerBySession(HttpServletRequest request) {
        Player player = (Player) request.getSession().getAttribute("player_login_data");
        if (null != player) {
            return player;
        } else {
            throw new BusinessException(ErrorEnums.E_5002);
        }
    }

    protected Boolean checkRoomOwnerIsLogin(HttpServletRequest request) {
        RoomOwner roomOwner = (RoomOwner) request.getSession().getAttribute("room_owner_login_data");
        return null != roomOwner;
    }

    protected Boolean checkPlayerIsLogin(HttpServletRequest request) {
        Player player = (Player) request.getSession().getAttribute("player_login_data");
        return null != player;
    }

    protected void setPlayerSession(HttpServletRequest request, Player player) {
        request.getSession().setAttribute("player_login_data", player);
    }

    protected RoomOwner getRoomOwnerBySession(HttpServletRequest request) {
        RoomOwner roomOwner = (RoomOwner) request.getSession().getAttribute("room_owner_login_data");
        if (null != roomOwner) {
            return roomOwner;
        } else {
            throw new BusinessException(ErrorEnums.E_6005);
        }
    }

    protected void setRoomOwnerSession(HttpServletRequest request, RoomOwner roomOwner) {
        request.getSession().setAttribute("room_owner_login_data", roomOwner);
    }
}
